package com.ssm.crud.service;

import com.ssm.crud.dao.EmployeeMapper;
import com.ssm.crud.entities.Employee;
import com.ssm.crud.entities.EmployeeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/11/15 16:17
 */
@Service
public class EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    public List<Employee> getAll() {
        EmployeeExample example = new EmployeeExample();
        example.setOrderByClause("emp_id");
        return employeeMapper.selectByExampleWithDept(example);
    }

    public void saveEmp(Employee employee) {
        employeeMapper.insertSelective(employee);
    }

    /**
     * 检验用户名是否可用
     *
     * @param userName
     * @return true：代表当前用户名可用 false代表不可用
     */
    public boolean checkEmpUserName(String userName) {
        EmployeeExample example = new EmployeeExample();
        example.createCriteria().andEmpNameEqualTo(userName);
        long count = employeeMapper.countByExample(example);
        return count == 0;
    }

    /**
     * 通过员工id查询员工信息
     *
     * @param id
     * @return
     */
    public Employee getEmployeeById(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKeyWithDept(id);
        return employee;
    }

    /**
     * 更新员工信息
     *
     * @param employee
     */
    public void updateEmp(Employee employee) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpIdEqualTo(employee.getEmpId());
        employeeMapper.updateByExampleSelective(employee, example);
    }

    /**
     * 根据员工主键删除指定的员工信息
     *
     * @param id
     */
    public void delEmp(Integer id) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpIdEqualTo(id);
        employeeMapper.deleteByExample(example);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    public void delBatchEmp(List<Integer> ids) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpIdIn(ids);
        employeeMapper.deleteByExample(example);
    }
}
